<?php

namespace App\Model;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

/**
 * App\Model\UserPopularity
 *
 * @property int $id
 * @property int $user_id 0游客
 * @property int $type 1名片 2个人微网 3营销网页
 * @property int $target_user_id 访问目标用户id
 * @property int $target_id 目标模版id
 * @property string $ip ip
 * @property \Illuminate\Support\Carbon $created_at 时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity whereIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity whereTargetId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity whereTargetUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity whereUserId($value)
 * @mixin \Eloquent
 * @property string|null $group_flag
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity whereGroupFlag($value)
 * @property int $status
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserPopularity whereStatus($value)
 */
class UserPopularity extends Model
{
	const CARD = 1; //名片
	const MICRO_WEB = 2;    //微网
	const WEB_PAGE = 3;    //营销网页

	protected $table = 'user_popularity';

	public $timestamps = false;

	/**
	 * @param int $type 类型
	 * @param int $target_user_id 目标用户id
	 * @param int $target_id 目标模版id
	 * @return bool
	 */
	public static function add($type, $target_user_id, $target_id): bool
	{
		$user_id = Auth::check() ? Auth::id() : 0;
		$ip = \request()->getClientIp();
		if ($user_id == $target_user_id) {
			return true;
		}
		if ($user_id > 0) {
			$log = self::where('type', $type)->where('user_id', $user_id)->where('target_id',$target_id)->orderByDesc('created_at')->first();
			if (!$log || ($log && !Carbon::parse($log->created_at)->isToday())) {
				return self::insert([
					'user_id' => $user_id,
					'type' => $type,
					'target_user_id' => $target_user_id,
					'target_id' => $target_id,
					'ip' => $ip,
					'group_flag'=>$user_id,
					'created_at' => Carbon::now()
				]);
			}
		} else {
			$log = self::where('type', $type)->where('user_id', 0)->where('target_id',$target_id)->where('ip', $ip)->orderByDesc('created_at')->first();
			if (!$log || ($log && !Carbon::parse($log->created_at)->isToday())) {
				return self::insert([
					'user_id' => $user_id,
					'type' => $type,
					'target_user_id' => $target_user_id,
					'target_id' => $target_id,
					'ip' => $ip,
					'group_flag'=>$ip,
					'created_at' => Carbon::now()
				]);
			}
		}
		$models = [self::CARD => 'App\Model\Cards', self::MICRO_WEB => 'App\Model\UserTemplate', self::WEB_PAGE => 'App\Model\UserMarketing'];
		$pop = [self::CARD => 'popularity', self::MICRO_WEB => 'popularity', self::WEB_PAGE => 'pop'];
		if (!empty($models[$type])) {
		    $model = new $models[$type];
		    $model->find($target_id);
		    $model->increment($pop[$type]);
        }
		return true;
	}
}
